﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Web;
using System.Web.Mvc;

namespace Guline.Hoster.Controllers
{
    public class FTPAccountController : BaseController
    {
        //
        // GET: /FTPAccout/

        public ActionResult Index()
        {
            return View(this.db.FTPAccounts.ToList());
        }
        public ActionResult Create()
        {
            ViewBag.OrganizeID = new SelectList(this.db.Organizes, "ID", "Name");
            return View();
        }
        [HttpPost]
        public ActionResult Create(Guline.HosterModels.Object.FTPAccount model)
        {
            ViewBag.OrganizeID = new SelectList(this.db.Organizes, "ID", "Name");
            if (ModelState.IsValid)
            {
                this.db.FTPAccounts.Add(model);
                this.db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View();
        }
        public ActionResult Edit(int ID)
        {
            var FTPAccount = db.FTPAccounts.Where(m => m.ID == ID).FirstOrDefault();
            ViewBag.OrganizeID = new SelectList(this.db.Organizes, "ID", "Name", selectedValue: FTPAccount.OrganizeID);
            return View(FTPAccount);
        }
        [HttpPost]
        public ActionResult Edit(Guline.HosterModels.Object.FTPAccount model)
        {
            ViewBag.OrganizeID = new SelectList(this.db.Organizes, "ID", "Name", selectedValue: model.OrganizeID);
            if (ModelState.IsValid)
            {
                db.Entry(model).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View();
        }
        public ActionResult Delete(int ID)
        {
            var FTPAccount = db.FTPAccounts.Where(m => m.ID == ID).FirstOrDefault();
            db.FTPAccounts.Remove(FTPAccount);
            db.SaveChanges();
            return RedirectToAction("Index");
        }
        public ActionResult editStatus(int ID)
        {
            var FTPAccount = db.FTPAccounts.Where(m => m.ID == ID).FirstOrDefault();
            if (FTPAccount.Status == 0)
            {
                FTPAccount.Status = 1;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            else
            {
                FTPAccount.Status = 0;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
        }
    }
}
